<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>滑动验证 - layui滑动验证</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/css/layui.css"/>
    <style>
        .pro_name a {
            color: #4183c4;
        }

        .osc_git_title {
            background-color: #fff;
        }

        .osc_git_box {
            background-color: #fff;
        }

        .osc_git_box {
            border-color: #E3E9ED;
        }

        .osc_git_info {
            color: #666;
        }

        .osc_git_main a {
            color: #9B9B9B;
        }

        #center_table {
            height: 400px;
            width: 650px;
            border: 1px solid black;
            margin: 200px auto;
            padding: 100px 120px 100px 100px;
            box-shadow: 15px 15px 15px 15px #888888;
        }
    </style>
</head>
<body>
<div id="center_table">
    <div class="layui-container">
        <div class="layui-row" style="margin-top: 50px;">
            <div class="layui-col-md6">
                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-block">
                            <input type="text" name="username" required lay-verify="required" placeholder="请输入用户名"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input type="text" name="password" required lay-verify="required" placeholder="请输入密码"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">滑动验证</label>
                        <div class="layui-input-block">
                            <div id="slider"></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                            <button class="layui-btn" type="button" id="reset">重置滑块</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>

        </div>
    </div>
</div>
</body>
<script src="layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
    layui.config({
        base: 'dist/sliderVerify/'
    }).use(['sliderVerify', 'jquery', 'form'], function () {
        var sliderVerify = layui.sliderVerify,
            $ = layui.jquery,
            form = layui.form;
        var slider = sliderVerify.render({
            elem: '#slider',
            onOk: function () {//当验证通过回调
                layer.msg("滑块验证通过");
            }
        });
        $('#reset').on('click', function () {
            slider.reset();
        });
        //监听提交
        form.on('submit(formDemo)', function (data) {
            if (slider.isOk()) {
                $.ajax({
                    url: "/user/login",
                    data: JSON.stringify(data.field),
                    contentType: "application/json",
                    type: "POST",
                    success: function (result) {
                        // 登录成功进行页面跳转
                        if (result.code === "1") {
                            window.location = "/index.html";
                        } else {
                            // 登录失败给出提示信息
                            layer.alert(result.message, {
                                title: '提示信息'
                            });
                        }
                    }
                });
            } else {
                layer.msg("请先通过滑块验证");
            }
            return false;
        });

    })
</script>

</html>